package org.hhgzs.modules.erp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hhgzs.modules.erp.constants.BusinessConstants;
import org.hhgzs.modules.erp.entity.Material;
import org.hhgzs.modules.erp.entity.MaterialCurrentStock;
import org.hhgzs.modules.erp.entity.MaterialInitialStock;
import org.hhgzs.modules.erp.exception.JshException;
import org.hhgzs.modules.erp.mapper.MaterialMapper;
import org.hhgzs.modules.erp.service.*;
import org.hhgzs.modules.erp.vo.MaterialVo;
import org.hhgzs.modules.erp.vo.MaterialVo4Unit;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 产品表 服务实现类
 * </p>
 *
 * @author peiqy
 * @since 2021-07-14
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    private final MaterialInitialStockService materialInitialStockService;

    private final MaterialCurrentStockService materialCurrentStockService;
    private final MaterialExtendService  materialExtendService;

    private final MaterialMapper materialMapper;

    @Override
    public IPage<MaterialVo4Unit> getMaterialEnableSerialNumberList(Page<MaterialVo4Unit> page, String q) {
        IPage<MaterialVo4Unit> list =null;
        try{
            list=  materialMapper.getMaterialEnableSerialNumberList(page,q);
        }catch(Exception e){
            JshException.readFail(log, e);
        }
        return list;
    }

    @Override
    public Long getMaterialEnableSerialNumberCount(String q) {
        return null;
    }

    /**
     * 根据商品和仓库获取初始库存
     *
     * @param materialId
     * @param depotId
     * @return
     */
    public BigDecimal getInitStock(Long materialId, Long depotId) {
        QueryWrapper<MaterialInitialStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MaterialInitialStock::getMaterialId, materialId).eq(MaterialInitialStock::getDepotId, depotId)
                .eq(MaterialInitialStock::getDeleteFlag, BusinessConstants.DELETE_FLAG_DELETED);
        MaterialInitialStock materialInitialStock = materialInitialStockService.getOne(queryWrapper);
        if (ObjectUtil.isNotNull(materialInitialStock)) {
            return materialInitialStock.getNumber();
        }
        return BigDecimal.ZERO;
    }

    /**
     * 根据商品和仓库获取当前库存
     *
     * @param materialId
     * @param depotId
     * @return
     */
    public BigDecimal getCurrentStock(Long materialId, Long depotId) {
        QueryWrapper<MaterialCurrentStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MaterialCurrentStock::getMaterialId, materialId).eq(MaterialCurrentStock::getDepotId, depotId)
                .eq(MaterialCurrentStock::getDeleteFlag, BusinessConstants.DELETE_FLAG_DELETED);
        MaterialCurrentStock materialCurrentStock = materialCurrentStockService.getOne(queryWrapper);
        if (ObjectUtil.isNotNull(materialCurrentStock)) {
            return materialCurrentStock.getCurrentNumber();
        }else {
            return  getInitStock(materialId, depotId);
        }
    }

    @Override
    public List<MaterialVo4Unit> getMaterialByBarCode(String barCode) {
        return materialMapper.getMaterialByBarCode(barCode);
    }

    @Override
    public int checkIsExist(Long id, String name, String model, String color, String standard, String mfrs, String otherField1, String otherField2, String otherField3, String unit, Long unitId) {
        return materialMapper.checkIsExist(id, name, model, color, standard, mfrs, otherField1,
                otherField2, otherField3, unit, unitId);
    }

    @Override
    public boolean update(MaterialVo data) {
        Material material = new Material();
        BeanUtil.copyProperties(data,material);
        this.updateById(material);
        data.getMeList().forEach(item->{
            item.setMaterialId(material.getId());
            materialExtendService.saveOrUpdate(item);
        });
        return true;
    }

    @Override
    public boolean save(MaterialVo data) {
        Material material = new Material();
        BeanUtil.copyProperties(data,material);
        this.save(material);
        data.getMeList().forEach(item->{
            item.setMaterialId(material.getId());
            materialExtendService.save(item);
        });
        return true;
    }
}
